package com.magist.manage.web.api.controller;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.magist.core.controller.BaseController;
import com.magist.core.entity.ResultMessage;
import com.magist.core.util.BeanCopyUtil;
import com.magist.core.util.DateUtil;
import com.magist.core.util.MAssertUtil;
import com.magist.core.util.TxtUtil;
import com.magist.manage.enums.CaseTypeEnum;
import com.magist.manage.enums.ContactStatusEnum;
import com.magist.manage.system.dto.OffContactDto;
import com.magist.manage.system.dto.OffNewsDto;
import com.magist.manage.system.entity.OffCase;
import com.magist.manage.system.entity.OffContact;
import com.magist.manage.system.entity.OffNews;
import com.magist.manage.system.entity.OffTag;
import com.magist.manage.system.service.OffCaseService;
import com.magist.manage.system.service.OffContactService;
import com.magist.manage.system.service.OffNewsService;
import com.magist.manage.system.service.OffTagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Api(value = "魔匠首页接口", tags = {"魔匠首页接口"})
@RestController
@RequestMapping("/apiIndex")
public class IndexController extends BaseController {

    @Autowired
    private OffNewsService offNewsService;
    @Autowired
    private OffCaseService offCaseService;
    @Autowired
    private OffTagService offTagService;
    @Autowired
    private OffContactService offContactService;

    @ApiImplicitParams({
    })
    @ApiOperation(value="标签列表", notes="分页查询标签接口")
    @RequestMapping(value = "tagPage.do", method = RequestMethod.GET)
    public Object tagPage() {
        ResultMessage result = ResultMessage.success();
        OffTag offTag = new OffTag();
        PageInfo<OffTag> pageInfo = offTagService.page(offTag,getPageInfo());
        result.setContent(pageInfo);
        return result;
    }

    @ApiOperation(value="新闻公告列表", notes="新闻公告列表")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "newsPage.do", method = RequestMethod.GET)
    public Object newsPage() {
        ResultMessage result = ResultMessage.success();
        OffNews offNews = new OffNews();
        PageInfo<OffNews> pageInfo = offNewsService.page(offNews,getPageInfo());
        PageInfo<OffNewsDto> copy = BeanCopyUtil.copy(pageInfo, PageInfo.class);
        List<OffNews> list = pageInfo.getList();
        List<OffNewsDto> collect = list.stream().map(news -> {
            OffNewsDto dto = BeanCopyUtil.copy(news, OffNewsDto.class);
            Date newsDate = news.getNewsDate();
            String format = DateUtil.format(newsDate, DateUtil.yyyy__MM__dd);
            String year = format.substring(0, 4);
            String day = format.substring(5, format.length());
            dto.setYear(year);
            dto.setDay(day);
            dto.setNewsDate(format);
            return dto;
        }).collect(Collectors.toList());

        copy.setList(collect);
        result.setContent(copy);
        return result;
    }

    @ApiOperation(value="案例列表", notes="案例接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "caseType", value = "案例类型", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "tag", value = "标签", required = false, paramType = "query", dataType = "String"),
    })
    @RequestMapping(value = "casePage.do", method = RequestMethod.GET)
    public Object casePage(String caseType, String tag) {
        ResultMessage result = ResultMessage.success();
        OffCase offCase = new OffCase();
        // 允许通过tag模糊查询
        if(!StringUtils.isEmpty(tag)) {
            offCase.setTags("%"+tag+"%");
        }
        offCase.setCaseType(caseType);
        PageInfo<OffCase> pageInfo = offCaseService.page(offCase,getPageInfo());
        List<OffTag> offTags = offTagService.find(new OffTag());
        pageInfo.getList().forEach(oCase->{
            String tags = oCase.getTags();
            if(!StringUtils.isEmpty(tags)) {
                String[] split = tags.split("#");
                List<String> nameList = Lists.newArrayList();
                for (String s : split) {
                    OffTag offTag = offTagService.findByCode(s);
                    if(!ObjectUtils.isEmpty(offTag)) {
                        nameList.add(offTag.getTagName());
                    }
                }
                oCase.setTags(String.join("#",nameList));
            }
        });
        result.setContent(pageInfo);
        return result;
    }


    @ApiOperation(value="案例详情", notes="案例详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "案例id", required = true, paramType = "query", dataType = "String"),
    })
    @RequestMapping(value = "caseDetail.do", method = RequestMethod.GET)
    public Object caseDetail(String id) {
        ResultMessage result = ResultMessage.success();
        OffCase offCase = offCaseService.getById(id);
        result.setContent(offCase);
        return result;
    }

    @ApiOperation(value="提交联系方式", notes="提交联系方式接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "linkName", value = "姓名", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "linkTel", value = "电话", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "userRemark", value = "用户留言", required = false, paramType = "query", dataType = "String"),
    })
    @RequestMapping(value = "submitContact.do", method = RequestMethod.POST)
    public Object submitContact(OffContactDto offContactDto) {
        OffContact offContact = new OffContact();
        BeanUtils.copyProperties(offContactDto,offContact);
        // 校验手机号是否合法
        String linkTel = offContactDto.getLinkTel();
        MAssertUtil.isMobile(linkTel,"手机号不正确");
        // 设置时间和ip
        String ip = TxtUtil.getIp(getRequest(), getResponse());
        offContact.setLinkDate(new Date());
        // 同一个ip只能一天最多只能提交5次
        boolean isAttack = offContactService.isAttack(ip);
        if(isAttack) {
            return ResultMessage.error("一天最多只能提交5次");
        }
        offContact.setIp(ip);
        offContact.setStatus(ContactStatusEnum.UNLINK.getCode());
        offContact.setId(offContactService.getNewId());
        offContactService.insert(offContact);
        return ResultMessage.success();
    }

}
